<?php
if ( !class_exists( "Classes_Template" ) ) { 
/*

	Author:	Cedric McMillan Junior

	Date:	March 15th 2012
	
	Aim:
			Basic template class that can handle:
	
			-	template and general code to be as seperate as possible (split code and design)
			-	single set of templates for many languages, so that if design changes, not lots of work
			-	caching of templates
			-	language versions of templates (minor localization)
	
		
	To Do:
	
			-	add header information that inserts correct ISO and meta data.
			-	add some exceptions


*/


class Classes_Template
{
	private	$output		=	0;
    
    # this can be used if we are caching 
    private	$language	=	"en";

    # class pointer
    private	$cacheObject;
    
	# tags to be substituted
    private $tags		=	array();
    

	# constructs the template filename and then gets from the cache or the disk
	private function getTemplate($templateFile,$language)
	{
		# prepare the filename for the cache	
		$cacheFile		=	$language	.	"."		.	$templateFile;

		# this is now the raw template ready for processing before output
		$this->output	=	$this->cacheObject->getOrSet(CACHE_TIME_TINY,"tmpl",$cacheFile, "loadFile","$templateFile");
		
		
		$this->substituteLabels($this->output);

	}





	# constructor gets the file from disk and also passes a language variable (used with caching)
	public function __construct(&$cacheObject,$templateFilename="",$templateCacheDir="",$language="en")
	{
		# create a reference to the cache object
		$this->cacheObject		=	$cacheObject;

		# test for bounds and set language for localization
		# *** IMPROVE: ideally you want to keep this seperated from the class - eg. pass in the languages elsewhere ***
		if	(!in_array($language,array("en","es","br")))
		{
			$language		=	"en";
		}

		$this->language			=	$language;

		if	(!empty($templateFilename))
		{
			$this->getTemplate($templateFilename,$language);
		}
    }




 	# basic version puts in a default file, but we can override
	public function subHeader()
	{
		$tags	=	
			array(
					# i would prefer a general passing of the directory
					"{{header}}"			=>	$this->cacheObject->getOrSet(CACHE_TIME_TINY, "template","header.html", "loadFile","data/templates/header.html"),
				);
		
		$this->subTagsInTemplate($tags);
 	}	





 	# puts in footer
	public function subFooter()
	{
		$tags	=	
			array(
			
					"{{footer}}"			=>	$this->cacheObject->getOrSet(CACHE_TIME_TINY, "template","footer.html", "loadFile","data/templates/footer.html"),
				);
		
		$this->subTagsInTemplate($tags);
 	}	





	# substitutes the tags we provide into the template
	public function subTagsInTemplate($tags)
	{
		foreach ($tags as $key => $value)
		{
        	$this->output = str_replace ($key, $value, $this->output);
        }
 	}







	# displays content of page
    public function display()
    {
		# substitute all the labels from the global language array
		$this->substituteLabels($this->output);

        echo	$this->output;

		# handles display for pqp debugger
		if	(PQP_DEBUG == true)
		{
			global	$pqp;
			
			$pqp->display();
		}

    }



 


	# substitutes the labels -	pass the reference
	private function substituteLabels(&$output)
	{
		# this array is important as it contains localized text and will be used in many files
		global	$gLanguageString;

		# finds all labels within the template and places them into resultant array
		preg_match_all('/#(.*?)#/', $output,$labelArray,0);

		# once found, strip out the label and other delimiters
		$num	=	count($labelArray[1]);

		for($i=0;$i<$num;$i++)
		{
			# this has the language as a direct array 
			if	(isset($gLanguageString[$labelArray[1][$i]]) == true)
			{
				# this should do the replacement
	       		$output = str_replace ($labelArray[0][$i], $gLanguageString[           $labelArray[1][$i]              ], $output);
			}
		}
		return	$num;
 	}







}












































}


?>